package m.jclecai.com.ui;

import android.content.Intent;
import android.net.http.SslError;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.contents.LotteryType;
import m.jclecai.com.contents.TrendChartUrl;

/**
 * Created by hp on 2017/3/14.
 */

public class TrendTransferActivity extends BaseActivity {

    @BindView(R.id.webview)
    WebView webview;

    @Override
    protected int getContentView() {
        return R.layout.webview_list_activity;
    }

    @Override
    protected void initView() {
        setTitle("走势图");
        leftOptionText("");
        rightOptionText("走势",R.mipmap.icon_009);
        allowBodyCoverTitle(true);
        showLoadingLayout(webview);
        WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);  //支持javaScript
        settings.setUseWideViewPort(true);//适应分辨率
//        settings.setAppCacheEnabled(true);
//        settings.setDatabaseEnabled(true);
//        settings.setDomStorageEnabled(true);//开启DOM缓存，关闭的话H5自身的一些操作是无效的
//        settings.setCacheMode(WebSettings.LOAD_DEFAULT);

        settings.setSupportZoom(false);
        settings.setDisplayZoomControls(false);
        webview.setHorizontalScrollBarEnabled(false);//水平不显示
        webview.setVerticalScrollBarEnabled(false); //垂直不显示
        settings.setBuiltInZoomControls(false);
//			webView_list.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        settings.setLoadsImagesAutomatically(true);  //支持自动加载图片
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); //支持内容重新布局
        webview.setWebViewClient(new WebViewClient() {
            boolean loadSuccess = true;

            @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
                super.onReceivedHttpError(view, request, errorResponse);
                showInternetOffLayout();
                loadSuccess = (errorResponse.getStatusCode() == 200);
            }


            @RequiresApi(Build.VERSION_CODES.M)
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                showInternetOffLayout();
                loadSuccess = (error.getErrorCode() == 200);
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                super.onReceivedSslError(view, handler, error);
                showInternetOffLayout();
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                loadSuccess = (errorCode == 200);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if(loadSuccess)
                    closeLoadingLayout();
                else
                    showInternetOffLayout();
                loadSuccess = true;
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if(url.contains("hotCode.htm")){
                    rightOptionText("冷热");
                }else if(url.contains("bounsCode.htm")){
                    rightOptionText("走势");
                }
                return false;
            }
        });
    }

    @Override
    protected void initData() {
        StringBuilder url = new StringBuilder(TrendChartUrl.BaseChartUrl);
        Intent intent = getIntent();
        int lotteryType = intent.getIntExtra("lotteryType", 0);

        switch (lotteryType) {
            case LotteryType.SSQ:
                url.append(LotteryType.SSQ);
                setTitle("双色球");
                break;
            case LotteryType.DLT:
                url.append(LotteryType.DLT);
                setTitle("大乐透");
                break;
            case LotteryType.QLC:
                url.append(LotteryType.QLC);
                setTitle("七乐彩");
                break;
            case LotteryType.PLW:
                url.append(LotteryType.PLW);
                setTitle("排列五");
                break;
            case LotteryType.PLS:
                url.append(LotteryType.PLS);
                setTitle("排列三");
                break;
            case LotteryType.SD:
                url.append(LotteryType.SD);
                setTitle("3D");
                break;
            case LotteryType.QXC:
                url.append(LotteryType.QXC);
                setTitle("七星彩");
                break;
            case LotteryType.CQSSC:
                url.append(LotteryType.CQSSC);
                setTitle("时时彩");
                break;
            case LotteryType.SDXYXW:
                url.append(LotteryType.SDXYXW);
                setTitle("十一选五");
                break;
        }
        webview.loadUrl(url.toString());
    }

    //七乐彩 时时彩


    @Override
    public void OnRetry(String btnMessage) {
        super.OnRetry(btnMessage);
        showLoadingLayout(webview);
        webview.reload();
    }
}
